package com.myride;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Utils {
	public static final String DEFAULT_PROPERTIES_FILE = "myride.properties";
	public static final String DEFAULT_LOG4J_FILE = "log4j.properties";
	public static final String DEFAULT_LOG_FILE = "myride.log";
		
	static Properties props;
	
	public static boolean configureLog4j(String configFileName, String loggerName) throws IOException {				
		File logPropsFile = new File(configFileName);
		if (!logPropsFile.exists()) {
			throw new FileNotFoundException("Log4j properties file not found.");
		}
		
		Properties logProps = new Properties();
		logProps.load(new FileInputStream(logPropsFile));
		
		// Configure Log4j				
		PropertyConfigurator.configure(logProps);							
		
		return true;
	}
	
	public static void readProperties(String path) throws IOException {
		File propsFile = new File(path);
		
		// Make sure properties file exists
		if (!propsFile.exists()) {
			throw new FileNotFoundException("Properties file not found.");
		}
		
		// Load and read the properties into member variables
		props = new Properties();
		props.load(new FileReader(propsFile));
	}
	
	public static String getProperty(String propName) {
		return props.getProperty(propName);
	}
	
	public static void postMail(String recipients[ ], String subject, String message) throws MessagingException, UnsupportedEncodingException
	{
		String SMTP_HOST_NAME = "smtp.gmail.com";
		String SMTP_PORT = "465";		
		final String SENDER_EMAIL = "myRideTau@gmail.com";
		final String SENDER_PASSWORD = "bokertov";
		String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";		
		
		boolean debug = false;

	    // Set the host smtp address
		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.put("mail.mime.charset", "UTF-8");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {

				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(SENDER_EMAIL, SENDER_PASSWORD);
				}
		});

		session.setDebug(debug);

		MimeMessage msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(SENDER_EMAIL, "myRide");
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
		addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		msg.setSubject(subject, "UTF-8");		
		msg.setContent(message, "text/html;charset=UTF-8");
		Transport.send(msg);
		
	}
	

}
